﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using BMC.ARSystem;

namespace Wicresoft.EKA.ITSM
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //args是参数
            //args[0]表示操作类型：-i | -u
            //-i:表示插入
            //-u:表示更新
            //args[1]是字段具体的值，用$分割，也就是说字段的内容不能有$
            //test : -i 33859$OPEN$CRITICAL$10.188.122.115$服务器$FOSS$2$Windows主机(10.188.122.115)的逻辑磁盘(C:)空间剩余率为(39.00%) 告警阀值为(<=90.0%)。$BPPM$BPPM$192.168.67.127:1828
            //test : -u 33859$CLOSED$CRITICAL$10.188.122.115$服务器$FOSS$2$Windows主机(10.188.122.115)的逻辑磁盘(C:)空间剩余率为(39.00%) 告警阀值为(<=90.1%)。$BPPM$BPPM$192.168.67.127:1828
            string form = ConfigurationManager.AppSettings["form"];
            string fieldIds = ConfigurationManager.AppSettings["fieldIds"];
            string keyFieldIds = ConfigurationManager.AppSettings["keyFieldIds"];
            string server = ConfigurationManager.AppSettings["server"];
            string user = ConfigurationManager.AppSettings["user"];
            string pwd = ConfigurationManager.AppSettings["pwd"];
           
            if (string.IsNullOrEmpty(server)
                || string.IsNullOrEmpty(user)
                || string.IsNullOrEmpty(pwd)
                || string.IsNullOrEmpty(form)
                || string.IsNullOrEmpty(fieldIds)
                )
            {
                Console.WriteLine("config missing.");
                return;
            }


            string[] fields = fieldIds.Split(',');
            string[] keys = keyFieldIds.Split(',');

            if (args.Length < 2)
            {
                Usage();
                return;
            }

            string valueStr = string.Empty;
            for (int i = 1; i < args.Length; i++)
            {
                valueStr += args[i] + " ";
            }
            valueStr.TrimEnd(' ');

            string op = args[0];
            string[] values = valueStr.Split('$');

            if (op == "-i")
            {
                Server _server = new Server();
                try
                {
                    _server.Login(server, user, pwd);

                    FieldValueList fl = new FieldValueList();
                    int count = 0;

                    foreach (string fid in fields)
                    {

                        fl.Add(Convert.ToUInt32(fid), values[count]);
                        count++;

                    }

                    _server.CreateEntry(form, fl);

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    _server.Logout();
                }

            }
            else if (op == "-u")
            {
                Server _server = new Server();
                try
                {
                    _server.Login(server, user, pwd);

                    string qulification = string.Empty;
                    FieldValueList fl = new FieldValueList();

                    foreach (var key in keys)
                    {
                        qulification += "'" + key + "'=\"" + values[fields.ToList().IndexOf(key)] + "\" AND ";
                    }

                    qulification += " 1=1 ";

                    int count = 0;
                    foreach (string fid in fields)
                    {
                        if (!keys.Contains(fid))
                        {
                            fl.Add(Convert.ToUInt32(fid), values[count]);
                        }
                        count++;
                    }

                    _server.SetEntryByQuery(form, qulification, true, Server.SetEntryByQuery_NoMatchOption.NoAction, Server.SetEntryByQuery_MultiMatchOption.ModifyAll, fl);

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                    _server.Logout();

                }
            }
            else
            {
                Usage();
                Console.Read();
                return;
            }

        }

        static void Usage()
        {
            Console.WriteLine(@"            //args是参数
            //args[0]表示操作类型：-i | -u
            //-i:表示插入
            //-u:表示更新
            //args[1]是字段具体的值，用$分割，也就是说字段的内容不能有$");
        }
    }
}
